﻿using PosClient.Model;
using PosClient.Service;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using static PosClient.ViewModel.MainViewModel;

namespace PosClient.ViewModel
{
    public class ChangePluPriceViewModel : ReactiveObject
    {
        private bool _IsDiscount;
        public bool IsDiscount
        {
            get => _IsDiscount;
            set => this.RaiseAndSetIfChanged(ref _IsDiscount, value);
        }

        private string _PriceInput;
        public string PriceInput
        {
            get => _PriceInput;
            set => this.RaiseAndSetIfChanged(ref _PriceInput, value);
        }

        private decimal _Price;
        public decimal Price
        {
            get => _Price;
            set => this.RaiseAndSetIfChanged(ref _Price, value);
        }

        public ReactiveCommand<Unit, Result<Plu>> ChangePriceCommand { get; }

        public MainViewModel ParentViewModel { get; set; }

        public ChangePluPriceViewModel(MainViewModel parentViewModel)
        {
            ParentViewModel = parentViewModel;

            var canChange = this.WhenAnyValue(vm => vm.Price, (x) => x >= 0 && parentViewModel.SelectPlu != null);

            this.WhenAnyValue(x => x.ParentViewModel.SelectPlu.Price)
                .Select(x => x)
                .Do(x =>
                {
                    if (!IsDiscount)
                    {
                        PriceInput = "100";
                    }
                    else
                    {
                        PriceInput = x.ToString();
                    }
                })
                .Subscribe();

            this.WhenAnyValue(x => x.PriceInput).Subscribe(x =>
            {
                decimal.TryParse(x, out decimal price);
                Price = price;
            });

            ChangePriceCommand = ReactiveCommand.Create(ChangePriceCommandImpl);
        }


        private Result<Plu> ChangePriceCommandImpl()
        {
            Result<Plu> result = new Result<Plu> { Success = true };
            if (ParentViewModel.SaleMode == SaleModeEnum.AfterSale &&
                ParentViewModel.RefundMode == RefundModeEnum.Order)
            {
                result.Success = false;
                result.Message = "整单退货不允许修改数量！";
                return result;
            }
            var plu = ParentViewModel.SelectPlu;
            // 赠品商品不能改价
            if (plu.Tag == Plu.PluTag.Present)
            {
                result.Message = "削价或者赠送商品不允许改价";
                result.Success = false;
                return result;
            }
            // 没有修改单价，不处理
            if (Price == plu.OriginPrice || (!IsDiscount && Price == 100))
            {
                result.Success = false;
                return result;
            }
            if (plu.OriginPrice > 0 && plu.OriginPrice < (!IsDiscount ? plu.Price * (Price / 100) : Price))
            {
                result.Message = "商品价格不能大于原售价或商品价格为0不允许修改";
                result.Success = false;
                return result;
            }
            if (result.Success)
            {
                decimal oldPrice = plu.Price;
                plu.Price = Math.Round(!IsDiscount ? plu.Price * (Price / 100) : Price, 2);
                plu.Tag = Plu.PluTag.Change;
                plu.Amount = plu.Count * plu.Price;
                plu.DisAmount = plu.OriginAmount - plu.Amount;
                ParentViewModel.SummaryCommand.Execute().Subscribe();
                //ParentViewModel.TotalAmount = ParentViewModel.GoodList.Sum(g => g.Amount);
                result.Data = (Plu)ToolService.DeepCopy(plu);
                result.Data.Price = oldPrice;
            }

            return result;
        }
    }
}
